/**
 * This script contains tests for the FlyTED Sparql Endpoint.
 * @author Alistair Miles
 * @author Jun Zhao
 */
// convenience variables
var Assert = YAHOO.util.Assert;
var log = YAHOO.log;
var JSON = YAHOO.lang.JSON;
var sparqlns = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ";
sparqlns += "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> ";
sparqlns += "PREFIX xs: <http://www.w3.org/2001/XMLSchema#> ";
sparqlns += "PREFIX chado: <http://purl.org/net/chado/schema/> ";
sparqlns += "PREFIX syntype: <http://purl.org/net/flybase/synonym-types/> ";
sparqlns += "PREFIX fbutil: <http://purl.org/net/openflydata/flybase-utils/> ";
sparqlns += "PREFIX so: <http://purl.org/obo/owl/SO#> "; // The Sequence Ontology 
//sparqlns += "PREFIX dro: <http://imageweb.zoo.ox.ac.uk/drosophila#> ";
//sparqlns += "PREFIX flybase: <http://rodos.zoo.ox.ac.uk/2008/flyweb/ontologies/flybase/>";

var _tests = {
	
   	"test_has_feature":{
    	query:  "ASK { ?x rdf:type chado:Feature . }",
        expect: true,
    	message:"endpoint has no features"
    },
    
    "test_has_gene":{
        query:  "ASK { ?x rdf:type so:SO_0000704 . }",
        expect: true,
        message:"endpoint has no genes"
    },
    
    "test_has_dbxref":{
        query:  "ASK { ?x rdf:type chado:Dbxref . }",
        expect: true,
        message:"endpoint has no dbxrefs"
    },
    
    "test_has_organism":{
        query:  "ASK { ?x rdf:type chado:Organism . }",
        expect: true,
        message:"endpoint has no organisms"
    },
    
    "test_has_synonym":{
        query:  "ASK { ?x rdf:type chado:Synonym . }",
        expect: true,
        message:"endpoint has no synonyms"
    },
    
    "test_has_fullname":{
        query:  "ASK { ?x rdf:type syntype:FullName . }",
        expect: true,
        message:"endpoint has no full names"
    },
    
    "test_has_feature_with_properties":{
        query:  "ASK { " +
				"?x rdf:type chado:Feature ;" +
					"chado:uniquename ?un ;" +
					"chado:name ?n ;" +
					"chado:organism ?o ;" +
					"chado:synonym ?s ;" +
					"chado:dbxref ?d ;" +
					"fbutil:anyName ?an ;" +
					"fbutil:anyNameLowerCase ?anlc ." +
                "}",
        expect: true,
        message:"endpoint has no features with properties (uniquename, name, organism, synonym, dbxref, anyName, anyNameLowerCase)"
    },
    
    "test_has_feature_with_fullname":{
        query:  "ASK { " +
				"?x rdf:type chado:Feature ;" +
					"chado:synonym ?s ." +
				"?s rdf:type syntype:FullName ." +
                "}",
        expect: true,
        message:"endpoint has no features with properties (uniquename, name, organism, synonym, dbxref)"
    },

    "test_has_feature_with_anyname_Rbf":{
        query:  "ASK { " +
                "?x fbutil:anyName \"Rbf\"^^xs:string ." +
                "}",
        expect: true,
        message:"endpoint has no features with anyname Rbf"
    },

    "test_has_feature_with_anynamelc_rbf":{
        query:  "ASK { " +
                "?x fbutil:anyNameLowerCase \"rbf\"^^xs:string ." +
                "}",
        expect: true,
        message:"endpoint has no features with anyname lowercase rbf"
    },

    "test_has_no_feature_with_anynamelc_Rbf":{
        query:  "ASK { " +
                "?x fbutil:anyNameLowerCase \"Rbf\"^^xs:string ." +
                "}",
        expect: false,
        message:"endpoint has features with anyname lowercase Rbf"
    },
    
    
    "test_has_dbxref_with_properties":{
        query:  "ASK { ?x rdf:type chado:Dbxref ; chado:accession ?a . }",
        expect: true,
        message:"endpoint has no dbxrefs with properties (accession)"
    },
    
    "test_has_organism_with_properties":{
        query:  "ASK { " +
        	    "?x rdf:type chado:Organism ;" +
        	       "chado:genus ?g ;" +
        	       "chado:species ?s ;" +
        	       "chado:abbreviation ?a ;" +
        	       "chado:common_name ?c ." +
        	    "}",
        expect: true,
        message:"endpoint has no organisms with properties (genus, species, abbreviation, common_name)"
    }
    
};

var _testCase = {
	name: "FlyBase gene names SPARQL Endpoint Test Case"
};

// test endpoint exists
_testCase.testEndpointExists = function() {

	// define the callback object
	var callback = {
		success: testEndpointExistsResponseSuccess(this),
		failure: testEndpointExistsResponseFailure(this)
	};
	
	log("Make the request");
	var request = YAHOO.util.Connect.asyncRequest("GET", "flybase", callback, null);
	
	log("Suspend the test runner");
	this.wait();
	
	log("N.B. this code should never be reached.");
}

function buildTestCase() {
	// add ask query tests to testcase object
	for (var _testname in _tests) {
	    var _test = _tests[_testname];
        log("adding test "+_testname+" to testcase, query is: "+_test.query);
	    _testCase[_testname] = buildTestFunction(_testname, _test.query, _test.expect, _test.message);
	}
}

function buildTestFunction(testname, query, expect, message) {
	return function() {
        log("test: "+testname);
        testAskQuery("flybase", this, query, expect, message);
	}
}

/**
 * @class
 */
//function tests() {}
//
//function testEndpointExists(testcase) {
//
//	// define the callback object
//	var callback = {
//		success: testEndpointExistsResponseSuccess(testcase),
//		failure: testEndpointExistsResponseFailure(testcase)
//	};
//	
//	log("Make the request");
//	var request = YAHOO.util.Connect.asyncRequest("GET", "flybase", callback, null);
//	
//	log("Suspend the test runner");
//	testcase.wait();
//	
//	log("N.B. this code should never be reached.");
//}
//
//tests.testEndpointExists = testEndpointExists;

//tests.testEndpointHasGenes  = function( testcase ) {
//	var query = "ASK { ?gene rdf:type so:SO_0000704 }";
//	testAskQuery("flybase", testcase, query, true, "endpoint has no genes");
//}
//
//tests.testEndpointHasGenes = testEndpointHasGenes;

//function testEndpointHasGenesWithFlyBaseId (testcase) {
//	var query = "ASK { ?gene flybase:flybaseID ?flybaseId }";
//	testAskQuery("flybase", testcase, query, true, "Endpoint has no genes with flybaseID property.");
//}
//
//tests.testEndpointHasGenesWithFlyBaseId = testEndpointHasGenesWithFlyBaseId;

//function testEndpointHasNoGenesWithoutFlyBaseId (testcase) {
//	var query = "ASK { ?gene rdf:type flybase:Gene . OPTIONAL {?gene flybase:flybaseID ?flybaseId }. FILTER ( !bound(?flybaseId)) }";
//	testAskQuery("flybase", testcase, query, false, "Expected no genes without flybase ids.");	
//}
//
//tests.testEndpointHasNoGenesWithoutFlyBaseId = testEndpointHasNoGenesWithoutFlyBaseId;

//function testEndpointHasNoGenesWithMultipleFlyBaseId (testcase) {
//	var query = "ASK { ?gene flybase:flybaseID ?flybaseId1, ?flybaseId2 . FILTER ( ?flybaseId1 != ?flybaseId2 ) }";
//	testAskQuery("flybase", testcase, query, false, "Expected no genes with multiple flybase ids.");	
//}
//
//tests.testEndpointHasNoGenesWithMultipleFlyBaseId = testEndpointHasNoGenesWithMultipleFlyBaseId;

//function testEndpointHasGenesWithSymbol (testcase) {
//	var query = "ASK { ?gene flybase:symbol ?symbol }";
//	testAskQuery("flybase", testcase, query, true, "Endpoint has no genes with symbol property.");
//}
//
//tests.testEndpointHasGenesWithSymbol = testEndpointHasGenesWithSymbol;

//function testEndpointHasNoGenesWithoutSymbol (testcase) {
//	var query = "ASK { ?gene rdf:type flybase:Gene . OPTIONAL {?gene flybase:symbol ?symbol }. FILTER ( !bound(?symbol)) }";
//	testAskQuery("flybase", testcase, query, false, "Expected no genes without symbols.");	
//}
//
//tests.testEndpointHasNoGenesWithoutSymbol = testEndpointHasNoGenesWithoutSymbol;

//function testEndpointHasNoGenesWithMultipleSymbol (testcase) {
//	var query = "ASK { ?gene flybase:symbol ?symbol1, ?symbol2 . FILTER ( ?symbol1 != ?symbol2 ) }";
//	testAskQuery("flybase", testcase, query, false, "Expected no genes with multiple symbols.");	
//}
//
//tests.testEndpointHasNoGenesWithMultipleSymbol = testEndpointHasNoGenesWithMultipleSymbol;

//function testEndpointHasGenesWithAnnotationSymbol (testcase) {
//	var query = "ASK { ?gene flybase:annotationSymbol ?annotationSymbol }";
//	testAskQuery("flybase", testcase, query, true, "Endpoint has no genes with annotationSymbol property.");
//}
//
//tests.testEndpointHasGenesWithAnnotationSymbol = testEndpointHasGenesWithAnnotationSymbol;

//function testEndpointHasNoGenesWithoutAnnotationSymbol (testcase) {
//	var query = "ASK { ?gene rdf:type flybase:Gene . OPTIONAL {?gene flybase:annotationSymbol ?annotationSymbol } . FILTER ( !bound(?annotationSymbol)) }";
//	testAskQuery("flybase", testcase, query, false, "Expected no genes without annotationSymbol property.");	
//}
//
//tests.testEndpointHasNoGenesWithoutAnnotationSymbol = testEndpointHasNoGenesWithoutAnnotationSymbol;

//function testEndpointHasNoGenesWithMultipleAnnotationSymbol (testcase) {
//	var query = "ASK { ?gene flybase:annotationSymbol ?annotationSymbol1, ?annotationSymbol2 . FILTER ( ?annotationSymbol1 != ?annotationSymbol2 ) }";
//	testAskQuery("flybase", testcase, query, false, "Expected no genes with multiple annotationSymbol properties.");	
//}
//
//tests.testEndpointHasNoGenesWithMultipleAnnotationSymbol = testEndpointHasNoGenesWithMultipleAnnotationSymbol;

//function testEndpointHasGenesWithFullName (testcase) {
//	var query = "ASK { ?gene flybase:fullname ?fullname }";
//	testAskQuery("flybase", testcase, query, true, "Endpoint has no genes with fullname property.");
//}
//
//tests.testEndpointHasGenesWithFullName = testEndpointHasGenesWithFullName;

//function testEndpointHasNoGenesWithoutFullName (testcase) {
//	var query = "ASK { ?gene rdf:type flybase:Gene . OPTIONAL {?gene flybase:fullname ?fullname }. FILTER ( !bound(?fullname)) }";
//	testAskQuery("flybase", testcase, query, false, "Expected no genes without fullname property.");	
//}
//
//tests.testEndpointHasNoGenesWithoutFullName = testEndpointHasNoGenesWithoutFullName;

//function testEndpointHasNoGenesWithMultipleFullName (testcase) {
//	var query = "ASK { ?gene flybase:fullname ?fullname1, ?fullname2 . FILTER ( ?fullname1 != ?fullname2 ) }";
//	testAskQuery("flybase", testcase, query, false, "Expected no genes with multiple fullname properties.");	
//}
//
//tests.testEndpointHasNoGenesWithMultipleFullName = testEndpointHasNoGenesWithMultipleFullName;

//function testEndpointHasGenesWithSynonym (testcase) {
//	var query = "ASK { ?gene flybase:synonym ?synonym }";
//	testAskQuery("flybase", testcase, query, true, "Endpoint has no genes with synonym property.");
//}
//
//tests.testEndpointHasGenesWithSynonym = testEndpointHasGenesWithSynonym;

//function testEndpointHasNoGenesWithoutSynonym (testcase) {
//	var query = "ASK { ?gene rdf:type flybase:Gene . OPTIONAL {?gene flybase:synonym ?synonym }. FILTER ( !bound(?synonym)) }";
//	testAskQuery("flybase", testcase, query, false, "Expected no genes without synonym property.");	
//}
//
//tests.testEndpointHasNoGenesWithoutSynonym = testEndpointHasNoGenesWithoutSynonym;

//function testEndpointHasGenesWithAnyName (testcase) {
//	var query = "ASK { ?gene flybase:anyname ?anyname }";
//	testAskQuery("flybase", testcase, query, true, "Endpoint has no genes with anyname property.");
//}
//
//tests.testEndpointHasGenesWithAnyName = testEndpointHasGenesWithAnyName;

//function testEndpointHasNoGenesWithoutAnyName (testcase) {
//	var query = "ASK { ?gene rdf:type flybase:Gene . OPTIONAL {?gene flybase:anyname ?anyname }. FILTER ( !bound(?anyname)) }";
//	testAskQuery("flybase", testcase, query, false, "Expected no genes without anyname property.");	
//}
//
//tests.testEndpointHasNoGenesWithoutAnyName = testEndpointHasNoGenesWithoutAnyName;

//var flybaseTestCase = new YAHOO.tool.TestCase({
//
//	name: "FlyBase gene names SPARQL Endpoint Test Case",
//	
//	testEndpointExists : function() {
//		testEndpointExists(this);
//	},
//	
//	testEndpointHasGenes : function() {
//		testEndpointHasGenes (this);
//	},
//	
//	testEndpointHasGenesWithFlyBaseId : function() {
//		testEndpointHasGenesWithFlyBaseId (this); 
//	},
//
//	
//	testEndpointHasNoGenesWithoutFlyBaseId : function() {
//		testEndpointHasNoGenesWithoutFlyBaseId (this);
//	},
//	
//
//	testEndpointHasNoGenesWithMultipleFlyBaseId : function() {
//		testEndpointHasNoGenesWithMultipleFlyBaseId (this);
//	},
//	
//	testEndpointHasGenesWithSymbol : function() {
//		testEndpointHasGenesWithSymbol (this); 
//	},
//
//    /* This test fails: 
//     * two entries are returned without symbols, but don't seem to correspond
//     * actual Flybase genes:  CR41445 and CR33987.
//	testEndpointHasNoGenesWithoutSymbol : function() {
//		testEndpointHasNoGenesWithoutSymbol (this);
//	},
//     */ 	
//
//    testEndpointHasNoGenesWithMultipleSymbol : function() {
//		testEndpointHasNoGenesWithMultipleSymbol (this);
//	},
//    
//	
//	testEndpointHasGenesWithAnnotationSymbol : function() {
//		testEndpointHasGenesWithAnnotationSymbol (this); 
//	},
//	
//	
//	/* This test did not pass ... 
//	 * several genes without annotation symbols
//	testEndpointHasNoGenesWithoutAnnotationSymbol : function() {
//		testEndpointHasNoGenesWithoutAnnotationSymbol (this);
//	},*/
//
//	
//	/* This test did not pass ...
//	 * only one annotation symbol is current
//	 testEndpointHasNoGenesWithMultipleAnnotationSymbol : function() {
//		testEndpointHasNoGenesWithMultipleAnnotationSymbol (this);
//	},*/
//	
//	testEndpointHasGenesWithFullName : function() {
//		testEndpointHasGenesWithFullName (this); 
//	},
//
//	/* This test failed
//	 * lots of entries for alleles, and a few genes without full names
//	testEndpointHasNoGenesWithoutFullName : function() {
//		testEndpointHasNoGenesWithoutFullName (this);
//	},
//	*/
//
//	/* This test failed
//	 * only one is current
//	testEndpointHasNoGenesWithMultipleFullName : function() {
//		testEndpointHasNoGenesWithMultipleFullName (this);
//	},
//	*/
//	
//    testEndpointHasGenesWithSynonym : function() {
//		testEndpointHasGenesWithSynonym (this); 
//	},
//
//	/* This test failed
//	 * two entries are returned without synonyms, but don't seem to correspond
//     * actual Flybase genes:  CR41445 and CR33987.
//     * the same pair that does not have symbols
//	testEndpointHasNoGenesWithoutSynonym : function() {
//		testEndpointHasNoGenesWithoutSynonym (this);
//	},
//	*/
//
//	testEndpointHasGenesWithAnyName : function() {
//		testEndpointHasGenesWithAnyName (this); 
//	},
//
//	testEndpointHasNoGenesWithoutAnyName : function() {
//		testEndpointHasNoGenesWithoutAnyName (this);
//	},
//
//})

function setup() {
	var logReader = new YAHOO.tool.TestLogger("logger");
	YAHOO.log("Logger setup done");	
	buildTestCase();
}

function runTests() {
	YAHOO.log("Running tests");
	YAHOO.tool.TestRunner.clear();
	YAHOO.tool.TestRunner.add(new YAHOO.tool.TestCase(_testCase));
	YAHOO.tool.TestRunner.run();
}

function runTest(testName) {
	var _test = _tests[testName];
	if (typeof _test != "undefined" || _test) {
		var to = {
			name : testName
		};
		var fn = function() {
			testAskQuery("flybase", this, _test.query, _test.expect, _test.message);
		};
		to[testName] = fn;
		var testCase = new YAHOO.tool.TestCase(to);
		YAHOO.tool.TestRunner.clear();
		YAHOO.tool.TestRunner.add(testCase);
		YAHOO.tool.TestRunner.run();
	}
}

YAHOO.util.Event.onDOMReady(setup);
//YAHOO.util.Event.onDOMReady(runTests);

